package com.ch.dream.logic.longstory;

import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.ch.dream.R;
import com.ch.dream.activity.longstory.AddDreamActivity;
import com.ch.dream.activity.longstory.LongStoryContentActivity;
import com.ch.dream.activity.main.LoginActivity;
import com.ch.dream.asynctask.GetDreamRequestAsyncTask;
import com.ch.dream.asynctask.GetLongStoryRequestAsyncTask;
import com.ch.dream.base.BaseLogic;
import com.ch.dream.base.BaseRequestAsyncTask.RequestServerListener;
import com.ch.dream.bean.DreamstoryBean;
import com.ch.dream.bean.LongstoryBean;
import com.ch.dream.common.CommonData;
import com.ch.dream.common.CommonUserInfo;
import com.ch.dream.tools.UtilTools;
import com.ch.dream.tools.ViewTools;

public class LongStoryContentLogic extends BaseLogic {
	
	private TextView tvContent;
	private Button btnShare;
	private Button btnPrevious;
	private Button btnNext;
	private ProgressDialog loadingDialog;
	private String longStoryId;
	private Integer dreamId;
	private String content;
	private String strPrev = "启航梦";
	private String strNext = "无后续";
	public LongStoryContentLogic(LongStoryContentActivity activity) {
		super(activity);
	}

	public LongStoryContentLogic(LongStoryContentActivity activity, int layoutId) {
		super(activity, layoutId);
	}
	
	public void initViews(){
		tvContent = (TextView)activity.findViewById(R.id.tvContent);
		btnShare = (Button)activity.findViewById(R.id.btnShare);
		btnPrevious = (Button)activity.findViewById(R.id.btnPrevious);
		btnNext = (Button)activity.findViewById(R.id.btnNext);
	}
	
	public void initData(){
		Intent intent = activity.getIntent();
		longStoryId = intent.getStringExtra("longStoryId");
		dreamId = UtilTools.getIntSharedPreferences(activity,CommonData.DREAMID+longStoryId, "dreamId", 0);
	}
	
	public void init(){
		initViews();
		initData();
		if(dreamId==0){
			LongstoryBean bean =new LongstoryBean();
			bean.setId(Integer.parseInt(longStoryId));
			btnPrevious.setEnabled(false);
			btnPrevious.setText(strPrev);
			new GetLongStoryRequestAsyncTask(activity, new RequestServerListener() {
				@Override
				public void onRequestServerEnd(Object result) {
					loadingDialog.dismiss();
					if(result!=null){
						try{
							JSONObject jsonObject =(JSONObject)result;
							content ="标题："+jsonObject.getString("summary")+
									"\n正文："+jsonObject.getString("content");
							tvContent.setText(content);
							if(jsonObject.getInt("hasNext")==0){
								disableNext();
								btnPrevious.setText(strPrev);
								btnPrevious.setEnabled(false);
							}
						}catch (Exception e) {
							e.printStackTrace();
							ViewTools.showShortToast(activity,"网络错误");
						}
					}else{
						ViewTools.showShortToast(activity,"网络错误");
					}
				}
				
				@Override
				public void onRequestServerBegin() {
					loadingDialog = ViewTools.showLoading(activity,"提示", "加载数据中，请稍候...");
				}
			}).execute(bean);
		}else{
			DreamstoryBean bean =new DreamstoryBean();
			bean.setId(dreamId);
			bean.setLongStoryId(0);
			new GetDreamRequestAsyncTask(activity, new RequestServerListener() {
				@Override
				public void onRequestServerEnd(Object result) {
					loadingDialog.dismiss();
					if(result!=null){
						try{
							JSONObject jsonObject =(JSONObject)result;
							content = jsonObject.getString("content");
							tvContent.setText(content);
							dreamId = jsonObject.getInt("id");
							UtilTools.setIntSharedPreferences(activity,CommonData.DREAMID+longStoryId, "dreamId", dreamId);
							if(jsonObject.getInt("hasNext")==0){
								disableNext();
							}
						}catch (Exception e) {
							e.printStackTrace();
							ViewTools.showShortToast(activity,"网络错误");
						}
					}else{
						ViewTools.showShortToast(activity,"网络错误");
					}
				}
				
				@Override
				public void onRequestServerBegin() {
					loadingDialog = ViewTools.showLoading(activity,"提示", "加载数据中，请稍候...");
				}
			}).execute(bean);
		}
		
		btnNext.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DreamstoryBean bean =new DreamstoryBean();
				bean.setId(dreamId);
				bean.setLongStoryId(Integer.parseInt(longStoryId));
				new GetDreamRequestAsyncTask(activity, new RequestServerListener() {
					@Override
					public void onRequestServerEnd(Object result) {
						loadingDialog.dismiss();
						if(result!=null){
							try{
								JSONObject jsonObject =(JSONObject)result;
								content = jsonObject.getString("content");
								tvContent.setText(content);
								if(0 == jsonObject.getInt("id")){
									content ="标题："+jsonObject.getString("summary")+
											"\n正文："+jsonObject.getString("content");
									tvContent.setText(content);
									dreamId =0;
									disablePrev();
									UtilTools.setIntSharedPreferences(activity,CommonData.DREAMID+longStoryId, "dreamId", dreamId);
									ViewTools.showShortToast(activity, "没有后续梦境");
								}else if(dreamId == jsonObject.getInt("id")){
									ViewTools.showShortToast(activity, "没有后续梦境");
								}else{
									dreamId = jsonObject.getInt("id");
									UtilTools.setIntSharedPreferences(activity,CommonData.DREAMID+longStoryId, "dreamId", dreamId);
								}
								if(jsonObject.getInt("hasNext")==0){
									disableNext();
								}
							}catch (Exception e) {
								e.printStackTrace();
								ViewTools.showShortToast(activity,"网络错误");
							}
						}else{
							ViewTools.showShortToast(activity,"网络错误");
						}
					}
					
					@Override
					public void onRequestServerBegin() {
						loadingDialog = ViewTools.showLoading(activity,"提示", "加载数据中，请稍候...");
					}
				}).execute(bean);
			}
		});
		btnPrevious.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DreamstoryBean bean =new DreamstoryBean();
				bean.setId(-dreamId);
				bean.setLongStoryId(Integer.parseInt(longStoryId));
				new GetDreamRequestAsyncTask(activity, new RequestServerListener() {
					@Override
					public void onRequestServerEnd(Object result) {
						loadingDialog.dismiss();
						if(result!=null){
							try{
								JSONObject jsonObject =(JSONObject)result;
								if(0 == jsonObject.getInt("id")){
									content ="标题："+jsonObject.getString("summary")+
											"\n正文："+jsonObject.getString("content");
									tvContent.setText(content);
									dreamId =0;
									disablePrev();
									UtilTools.setIntSharedPreferences(activity,CommonData.DREAMID+longStoryId, "dreamId", dreamId);
								}else{
									content = jsonObject.getString("content");
									tvContent.setText(content);
									dreamId = jsonObject.getInt("id");
									UtilTools.setIntSharedPreferences(activity,CommonData.DREAMID+longStoryId, "dreamId", dreamId);
								}
							}catch (Exception e) {
								e.printStackTrace();
								ViewTools.showShortToast(activity,"网络错误");
							}
						}else{
							ViewTools.showShortToast(activity,"网络错误");
						}
					}
					
					@Override
					public void onRequestServerBegin() {
						loadingDialog = ViewTools.showLoading(activity,"提示", "加载数据中，请稍候...");
					}
				}).execute(bean);
			}
		});
		
		btnShare.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ViewTools.showShare(activity,"梦境传递真精彩：\n"+content+"\n来自故事梦android");
			}
		});
	}
	
	public void disablePrev(){
		btnPrevious.setEnabled(false);
		btnPrevious.setText(strPrev);
		btnNext.setEnabled(true);
		btnNext.setText("下一梦");
	}
	public void disableNext(){
		btnPrevious.setEnabled(true);
		btnPrevious.setText("上一梦");
		btnNext.setEnabled(false);
		btnNext.setText(strNext);
	}
	
	public void addDream(){
		if(!btnNext.getText().equals("无后续")){
			ViewTools.showShortToast(activity, "看完现有梦境才能续梦");
			return;
		}
		if(CommonUserInfo.userId==null || "".equals(CommonUserInfo.userId)){
			ViewTools.showShortToast(activity, "请先登录");
			Intent intent =new Intent(activity,LoginActivity.class);
			activity.startActivity(intent);
			activity.finish();
			return;
		}
		Intent intent =new Intent(activity,AddDreamActivity.class);
		intent.putExtra("longStoryId",longStoryId);
		if(content.length()>20){
			intent.putExtra("content", "..."+content.substring(content.length()-20, content.length()-1));
		}else{
			intent.putExtra("content",content);
		}
		activity.startActivity(intent);
	}
}
